<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Vue自定义事件</title>
    <script type="text/javascript" src="https://cdn.jsdelivr.net/npm/vue@2.5.21/dist/vue.min.js"></script>
</head>
<body>
<div id="app">
    <p>{{title}}</p>
    <ul>
        <li v-for="book in books">{{book}}</li>
    </ul>
    <hr/>
    <books>
        <books_title slot="books_title" :title="title"></books_title>
        <books_item slot="books_item" v-for="(book,index) in books" :item="book"
                    @remove="remove_book(index)"></books_item>
    </books>
</div>

<script>
    Vue.component("books_title", {
        props: ["title"],
        template: "<p>{{title}}</p>"
    });
    Vue.component("books_item", {
        props: ["item"],
        template: "<li>{{item}} <button @click='remove'>删除</button></li>",
        methods: {
            remove: function () {
                this.$emit("remove")
            }
        },
    });
    Vue.component("books", {
        template:
            "<div>" +
            "    <slot name='books_title'></slot>" +
            "    <ul>" +
            "        <slot name='books_item'></slot>" +
            "    </ul>" +
            "</div>"
    });
    let vue = new Vue({
        el: "#app",
        data: {
            title: "书籍",
            books: [
                "Java", "Linux", "Python", "前端"
            ]
        },
        methods: {
            remove_book: function (index) {
                this.books.splice(index, 1);
            }
        },
    })
</script>

</body>
</html>